<?php
class Absence {

   /**
    * A generated check time from absence machine
    *
    * @var string
    * @access protected
   */
   protected $checkTime = null;
   
   protected $absenceIn = null;
   protected $absenceOut = null;
   protected $overTimeBegin = null;

   /**
    * Part of on-duty time rules
    * This is a start limit of on-duty checktime.
    * A user is considered and approved to be present (hadir)
    * when its checktime >= $absenceInBegin
    *
    * @var string
    * @access protected
   */
   protected $absenceInBegin = null;

   /**
    * Part of on-duty time rules
    * @var string
    * @access protected
   */
   protected $absenceInEnd = null;

   /**
    * Part of off-duty time rules
    * This is a start limit of off-duty checktime.
    * A user is considered and approved to be leaving
    * when its checktime >= $absenceOutBegin
    *
    * @var string
    * @access protected
   */
   protected $absenceOutBegin = null;

   /**
    * Part of off-duty time rules
    * @var string
    * @access protected
   */
   protected $absenceOutEnd = null;

   /**
    * Part of on-duty time rules
    * A time tolerance for late user. If user's on-duty checktime
    * is equal $lateTimeTolerance, then user is still considered and approved
    * to be present.
    *
    * @var string
    * @access protected
   */
   protected $lateTimeTolerance = null;

   /**
    * Part of off-duty time rules
    * A time tolerance for leaving. If user's off-duty checktime
    * is >= $leaveTimeTolerance, then user is still considered and approved
    * to be off-duty.
    *
    * @var string
    * @access protected
   */
   protected $leaveTimeTolerance = null;
   
   /**
    * Late handler variables
   */
   protected $totalHoursOfLate = 0;
   
   /**
    * Leave Early handler variables
   */
   protected $totalHoursOfLeaveEarly = 0;

   /**
    * @param Array $workTime
    * $workTime = array(
    * )
    * 
   */
   public function __construct($workTime = array()) {
      $this->absenceIn = $workTime["absenceIn"];
      $this->absenceOut = $workTime["absenceOut"];
      $this->absenceInBegin = $workTime["absenceInBegin"];
      $this->absenceInEnd = $workTime["absenceInEnd"];
      $this->absenceOutBegin = $workTime["absenceOutBegin"];
      $this->absenceOutEnd = $workTime["absenceOutEnd"];
      $this->lateTimeTolerance = $workTime["lateTimeTolerance"];
      $this->leaveTimeTolerance = $workTime["leaveTimeTolerance"];
      $this->overTimeBegin = $workTime["overTimeBegin"];
   }

   
   /**
    * Get absenceIn attr value
   */
   public function getAbsenceIn() {
      return $this->absenceIn;
   }
   
   /**
    * Get absenceOut attr value
   */
   public function getAbsenceOut() {
      return $this->absenceOut;
   }
   
   /**
    * Get absenceInBegin attr value
   */
   public function getAbsenceInBegin() {
      return $this->absenceInBegin;
   }

   /**
    * Get absenceInEnd attr value
   */
   public function getAbsenceInEnd() {
      return $this->absenceInEnd;
   }
   
   /**
    * Get absenceOutBegin attr value
   */
   public function getAbsenceOutBegin() {
      return $this->absenceOutBegin;
   }

   /**
    * Get absenceOutEnd attr value
   */
   public function getAbsenceOutEnd() {
      return $this->absenceOutEnd;
   }

   /**
    * Get lateTimeTolerance attr value
   */
   public function getLateTimeTolerance() {
      return $this->lateTimeTolerance;
   }

   /**
    * Get leaveTimeTolerance attr value
   */
   public function getLeaveTimeTolerance() {
      return $this->leaveTimeTolerance;
   }
   
   public function getOverTimeBegin() {
      return $this->overTimeBegin;
   }

   /**
    * Checking user's checktime status. Whether a user meets the rule
    * of on-duty time
    *
    * @access public
    * @return boolean - true | false
   */
   public function isAbsenceIn($checkTime) {
      $ret = false;

      if ((strtotime($checkTime) >= strtotime($this->absenceInBegin)) && (strtotime($checkTime) <= strtotime($this->absenceInEnd))) {
         $ret = true;
      }

      return $ret;
   }
   
   
   /**
    * Checking user's checktime status. Whether a user meets the rule
    * of off-duty time
    *
    * @access public
   */
   public function isAbsenceOut($checkTime) {
      $ret = false;

      if ((strtotime($checkTime) >= strtotime($this->absenceOutBegin)) && (strtotime($checkTime) <= strtotime($this->absenceOutEnd))) {
         $ret = true;
      } else {
         if ((strtotime($checkTime) >= strtotime($this->leaveTimeTolerance)) && (strtotime($checkTime) <= strtotime($this->leaveTimeTolerance))) {
            $ret = true;
         }
      }

      return $ret;
   }

   /**
    * Test and check leave early condition
    *
    * @access public
    * @return boolean true | false
   */
   public function isLeaveEarly($checkTime) {
      $ret = false;

      if ((strtotime($checkTime) > strtotime($this->absenceOutBegin)) && (strtotime($checkTime) < strtotime($this->leaveTimeTolerance))) {
         $ret = true;
      }

      return $ret;
   }

   /**
    * Test and check come late condition
    *
    * @access public
    * @return boolean true | false
   */
   public function isComeLate($checkTime) {
      $ret = false;

      if (strtotime($checkTime) >= strtotime($this->lateTimeTolerance) && strtotime($checkTime) <= strtotime($this->absenceInEnd)) {
         $ret = true;
      }
      
      return $ret;
   }
   
   public function isOverTime($checkTime) {
      $ret = false;

      if (strtotime($checkTime) > strtotime($this->getOverTimeBegin())) {
         $ret = true;
      }
      
      return $ret;
   }

   public function calculateTotalHoursOfLate($lateType = 'COMPLETE', $checkTimeMin = '') {
      $timediff_late = null;
      
      // Ada absen IN dan ada absen OUT
      if ($lateType == 'COMPLETE') {
         $timediff_late = strtotime($checkTimeMin) - strtotime($this->getAbsenceIn());
      }
      
      // Ada absen OUT tetapi tidak ada absen IN
      if ($lateType == 'NO_IN') {
         $timediff_late = strtotime($this->getAbsenceInEnd()) - strtotime($this->getAbsenceIn());
      }
      
      $this->totalHoursOfLate = $this->totalHoursOfLate + $timediff_late;

      return $this->totalHoursOfLate;
   }
   
   public function calculateTotalHoursOfLeaveEarly($leaveEarlyType = 'COMPLETE', $checkTimeMax = '') {
      $timediff_leave_early = null;
      
      if ($leaveEarlyType == 'COMPLETE') {
         $timediff_leave_early = strtotime($this->getAbsenceOut()) - strtotime($checkTimeMax);
      }
      
      if ($leaveEarlyType == 'NO_OUT') {
         $timediff_leave_early = strtotime($this->getAbsenceOut()) - strtotime($this->getAbsenceOutBegin());
      }
      
      $this->totalHoursOfLeaveEarly = $this->totalHoursOfLeaveEarly + $timediff_leave_early;
      
      return $this->totalHoursOfLeaveEarly;
   }
}
